#include "SequenceGraph.h"
#include "base/Utility.h"
#define SetAttr(AttrName) if( ele->Attribute(AttrName)) SetAttribute(AttrName, ele->Attribute(AttrName));
bool SequenceGraph::OnInit()
{
    for (int i = 0;i < nextGid.size();i++)
    {
        GraphBase* loopG = context.FindGraphById(nextGid[i]);
        if (loopG != nullptr)
        {
            nextGs.push_back(loopG);
        }
        else {
            printf("SequenceGraph not find nextGraph[%d]:%d\r\n", i, nextGraphId);
        }
    }
    return true;
}

void SequenceGraph::ParseXmlElement(TiXmlElement* ele)
{
    // SetAttr("name")
    SetAttr("pos")
        SetAttr("size")
        SetAttr("seq0")
        SetAttr("seq1")
        SetAttr("seq2")
}

bool SequenceGraph::SetAttribute(const char* attrName, const char* attrVal)
{
    if (strcmp("id", attrName) == 0)
    {
        id.val = atoi(attrVal);
        graphId.val = atoi(attrVal);
    }
    else if (strcmp("name", attrName) == 0)
    {
        graphName = attrVal;
    }
    else if (strcmp("pos", attrName) == 0)
    {
        int posX, posY;
        sscanf_s(attrVal, "%d,%d", &posX, &posY);
        pos.x.val = posX;
        pos.y.val = posY;
    }
    else if (strcmp("size", attrName) == 0)
    {
        int x, y;
        sscanf_s(attrVal, "%d,%d", &x, &y);
        size.x.val = x;
        size.y.val = y;
    }
    else if (strcmp("seq0", attrName) == 0)
    {
        //nextGs.push_back(atoi(attrVal));
        nextGid.push_back(atoi(attrVal));
    }
    else if (strcmp("seq1", attrName) == 0)
    {
        //nextGraphId = atoi(attrVal);
        nextGid.push_back(atoi(attrVal));
    }
    else if (strcmp("seq2", attrName) == 0)
    {
        nextGid.push_back(atoi(attrVal));
        //nextGraphId = atoi(attrVal);
    }
    return true;
}

void SequenceGraph::OnDraw()
{
    if (opp.size() == 0) {
        for (int i = 0;i < nextGid.size();i++)
        {
            OutputPin* o = NewObj<OutputPin>();
            o->gbParent = this;
            o->p = &nextGs[i]->pin;
            opp.push_back(o);
        }
    }

    glPushMatrix();
    glTranslatef(baseOffset.x.val, baseOffset.y.val, zIndex.val);
    glLineWidth(1);
    DrawBorder();
    glColor3f(1, 1, 1);
    // -8,-4 8,4 18,0
    glVertex3f(pos.x.val + size.x.val - 20 - 8, pos.y.val + 15 - 4, 0);
    glVertex3f(pos.x.val + size.x.val - 20 + 8, pos.y.val + 15 - 4, 0);
    glVertex3f(pos.x.val + size.x.val - 20 + 18, pos.y.val + 15, 0);
    glVertex3f(pos.x.val + size.x.val - 20 + 8, pos.y.val + 15 + 4, 0);
    glVertex3f(pos.x.val + size.x.val - 20 - 8, pos.y.val + 15 + 4, 0);
    glEnd();
    glColor3f(1, 1, 1);
    std::wstring wstr = Utility::ConvertToWstring(graphName.val);
    wchar_t* str = const_cast<wchar_t*>(wstr.data());
    TrueTypeFont::GetInst()->drawText(str, pos.x.val + size.x.val / 4, pos.y.val + 50, zIndex.val, size.x.val, 20);
    int outNum = nextGid.size();
    for (int i = 0;i < outNum;i++)
    {
        opp[i]->pos.x = pos.x.val + size.x.val - 10;
        opp[i]->pos.y = pos.y.val + i * 6 + (i + 1) * size.y.val / (outNum + 1);
        opp[i]->Draw();
    }
    
    glPopMatrix();


}

void SequenceGraph::onMouseButtonDown(int button, int x, int y)
{
    __super::onMouseButtonDown(button, x, y);
}

void SequenceGraph::onMouseMove(int x, int y)
{
    __super::onMouseMove(x, y);
}

void SequenceGraph::onMouseButtonUp(int button, int x, int y)
{
    __super::onMouseButtonUp(button, x, y);
}

BlueprintScriptLibrary::Class* SequenceGraph::GetClass()
{
    return nullptr;
}

Vec2vbi SequenceGraph::GetOutputPinPos(int index)
{
    return Vec2vbi();
}

void SequenceGraph::Execute()
{
    for (int i = 0;i < nextGs.size();i++)
    {
        nextGs[i]->Execute();
    }
}

FunctionPin* SequenceGraph::GetFunctionPin()
{
    return nullptr;
}

void SequenceGraph::ConnectTo(GraphBase* graph, int index)
{
}
